# -*- coding: utf-8 -*-
'''
Created on 2013-4-12

@author: dyq
'''
from Models import  models
import  datetime,Cms
from Common.tool import tool

def General():
    '''
     普通用户
    '''
    return 1 
def Admin():
    '''
     管理员
    '''
    return 2 
def SuperAdmin():
    '''
    超级管理员
    '''
    return 3

class user:

    UserKey="UserKey"
   
    #用户等级枚举
    General,Admin,SuperAdmin = range(3)
    
    
    def Detect(self,request,purview):
        '''
                             权限处理
        '''  
        if self.IsLogin(request):
            return  self.GetUser(request).PurviewTeamID.id>=purview
        else:
            return False
            
    def IsLogin(self,request):
        '''
                            用户是否登录
        '''  
        return request.session.get(self.UserKey)!=None
        
        
    def Login(self,userModel,request):
        '''
                            用户登录
        '''
        request.session[self.UserKey]=userModel
        self.Log(request,0)
    def GetUser(self,request):
        '''
                             获取当前用户
        '''  
        return request.session.get(self.UserKey)
        
    def LogOut(self,request):
        '''
                             退出登录
        '''  
        request.session[self.UserKey]=None
        
        
        
    def Log(self,request,typeid,text=""):
        '''
                            写入日志
        '''  
        info=models.Sys_Log(LogTime=datetime.datetime.now(),
                            ConText=text,
                            UserId=request.session.get(self.UserKey),
                            LogId=tool.guid(),
                            TypeId=typeid)
        info.save()
        
        
        